/* 
 * File:   ComplexParameterCommand.cpp
 * Author: Gab'z
 * 
 * Created on 5 juin 2012, 12:27
 */

#include "ComplexParameterCommand.h"
#include "../../Context.h"
#include " ../../../../memento/BooleanParameterMemento.h"

namespace calculator {

    ComplexParameterCommand::ComplexParameterCommand(const std::string name) : ParametersCommand(name) {
    }

    ComplexParameterCommand::ComplexParameterCommand(const ComplexParameterCommand& orig) : ParametersCommand(orig._name) {
    }

    ComplexParameterCommand::~ComplexParameterCommand() {
    }

    ComplexParameterCommand* ComplexParameterCommand::clone() const {
        return new ComplexParameterCommand(_name);
    }

    void ComplexParameterCommand::apply(Parameters* parameters) const {
        parameters->setComplexMode(!parameters->getComplexMode());
    }

    const Memento* ComplexParameterCommand::createMemento() const throw (CommandException) {
        return new BooleanParameterMemento(clone(), Context::getInstance()->getParameters()->getComplexMode());
    }

    void ComplexParameterCommand::restoreFromMemento(const Memento* memento) const throw (MementoException) {
        const BooleanParameterMemento* booleanParameterMemento = dynamic_cast<const BooleanParameterMemento*> (memento);
        if (booleanParameterMemento == 0) {
            std::string str = "Bad Memento type, expected : BooleanParameterMemento, was ";
            str += typeid (*memento).name();
            throw MementoException(str);
        }
        Context::getInstance()->getParameters()->setComplexMode(booleanParameterMemento->_boolean);
    }

}